/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bakenote.core;

import java.text.NumberFormat;
import java.text.ParseException;

/**
 *
 * @author lello
 */
public class Amount {

	private boolean  isString = false;
	private boolean  isNull = false;
	private boolean  isNumber = false;
    private String unit = "g";
	//private static NumberFormat nf = NumberFormat.getNumberInstance(Locale.ITALIAN);
	//private static NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
	private static final NumberFormat nf = NumberFormat.getNumberInstance();

	private String symbol;
	private double weight;

	private Amount(double w) {
		//System.out.println("call amount double ");
		weight = w;
		isString = false;
		isNull = false;
		isNumber = true;
	}

	private Amount(String s) {
		//System.out.println("call amount string ");
		try {
			weight = nf.parse(s).doubleValue();
			weight = Double.parseDouble(s); //nf.parse(s).doubleValue();
			//System.out.println("parsed amount "+weight);
			isNumber = true;
			isNull = false;
		} catch (ParseException e) {
			symbol = s;
			//System.out.println("parsed unmeasurable amount "+weight);
			if (s.isEmpty() || s.equals("-")) {
				isString = false;
				isNull = true;
			} else {
				isString = true;
				isNull = false;
			}
		}
	}

	public static Amount valueOf(double amount) {
		return new Amount(amount);
	}

	public static Amount valueOf(String s) {
		return new Amount(s);
	}

	public boolean isMeasurable() {
		return (isNumber && weight > 0.0d);
	}

	public boolean isNull() {
		return isNull;
	}

	public String getType() {
		String s = "s";
		if (isNumber) s = "n";
		return s;
	}

	public double getDouble() {
		return weight;
	}

	public String getSymbol() {
		return symbol;
	}

	public boolean equals(double w) {
		if (isMeasurable()) {
			return (weight == w);
		} else {
			return false;
		}
	}

	public boolean equals(String s) {
		if (!isMeasurable()) {
			return (symbol.equals(s));
		} else {
			return false;
		}
	}

	@Override
	public String toString() {
		if (isNumber) {
			return Double.toString(weight);
		} else {
			return symbol;
		}
	}

}
